const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin')
module.exports = {
  entry: './src/index.js',
  devtool: false, //'source-map',
  mode: 'development',
  output: {
    filename: '[name]_bundle.js',
    path: path.resolve(__dirname, './build'),
    clean: true,
    // chunkFilename: '[name]_chunk.js'
  },
  externals: {
    jquery: 'jQuery',
  },
  // 优化配置
  optimization: {
    // 生成的chunkid算法
    // named  development 推荐
    // 默认的 deterministic production 推荐
    chunkIds:"deterministic",
    runtimeChunk:{ // 脚手架不配置了
      name:"runtime"
    },
    splitChunks: {
      // 分包
      chunks: 'all',
      // maxSize: 20 * 1024,
      // minSize: 10 * 1024,
      cacheGroups:{
        vendors:{
          test:/[\\/]node_modules[\\/]/,
          filename:"[id]_verdors.js"
        },
        routers:{
          test:/router/,
          filename:"[id]router_verdors.js"
        }
      }
    },

    minimizer:[
      new TerserPlugin({ extractComments:false ,parallel:4})
    ]
  },
 
  devServer: {
    // hot:true,
    static: ['public', 'vender'],
    compress: true,
    open: true,
    proxy: {
      '/api': {
        target: 'http://localhost:9000',
        pathRewrite: { '^/api': '' }
        // changeOrigin: true
      },
      port: 3000
    },
    historyApiFallback: true
  },
  module: {
    rules: [
      {
        test: /\.(m|e)?jsx?$/,
        use: [
          {
            loader: 'babel-loader'
          }
        ]
      },
      {
        test: /\.ts$/i,
        //use:'ts-loader'
        use: ['babel-loader']
      }
    ]
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: './index.html'
    })
  ]
};
